
#include <Windows.h>
#include <stdio.h>

void print_size(LPCWSTR name, ULONGLONG size);

int main()
{
    MEMORYSTATUSEX msex;
    msex.dwLength = sizeof(MEMORYSTATUSEX);
	GlobalMemoryStatusEx(&msex);
#define P(s) WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), s, sizeof(s) / sizeof(s[0]) - 1, NULL, NULL)
    P(L"内存使用率");
    printf(": %lu%%\n", msex.dwMemoryLoad);
    print_size(L"总内存", msex.ullTotalPhys);
    print_size(L"使用中内存", msex.ullTotalPhys - msex.ullAvailPhys);
    print_size(L"可用内存", msex.ullAvailPhys);
    print_size(L"总分页内存", msex.ullTotalPageFile);
    print_size(L"已提交", msex.ullTotalPageFile - msex.ullAvailPageFile);
    print_size(L"可用分页内存", msex.ullAvailPageFile);
    print_size(L"总虚拟内存", msex.ullTotalVirtual);
    print_size(L"已用虚拟内存", msex.ullTotalVirtual - msex.ullAvailVirtual);
    print_size(L"可用虚拟内存", msex.ullAvailVirtual);
    print_size(L"可用扩展虚拟内存", msex.ullAvailExtendedVirtual);
    return 0;
}

void print_size(LPCWSTR name, ULONGLONG size)
{
    double dsize = size;
    WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), name, wcslen(name), NULL, NULL);
    printf("%*s: ", (int)(20 - wcslen(name) * 2), "");
    if(size < 0x400)
    {
        printf("%llu B\n", size);
        return;
    }
    const char *pre = "KMGTPE";
    for (size_t i = 0; i < 6; i++)
    {
        dsize /= 0x400;
        if(i == 5 || dsize < 0x400)
        {
            printf("%.1f %cB\n", dsize, pre[i]);
            break;
        }
    }
}